package com.dzf.controller;

import com.dzf.entity.Hot;
import com.dzf.entity.RespJson;

import com.dzf.service.HotService;
import com.dzf.service.LargeFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author DZF
 * 热点轮播图的后台管理类
 */
@RestController
@RequestMapping("/api/adm/hot")
public class AdmHotController {
    @Autowired
    HotService hotService;
    @Autowired
    LargeFileService largeFileService;

    /**
     * 查询轮播图
     * @return 轮播图数据
     */
    @Cacheable(value = "book",keyGenerator = "springCacheDefaultKeyGenerator",cacheManager = "cacheManager1Hour")
    @RequestMapping("/findAll")
    public RespJson findAll(){

        return RespJson.pack(200,"OK",hotService.findAll());
    }

    /**
     * 添加轮播图
     * @param hot 轮播图的实体类，轮播图的信息
     * @param pic 图片
     * @return 添加成功查询数据
     */
    @CacheEvict(value = "book",allEntries = true)
    @RequestMapping("/addHot")
    public RespJson addHot(Hot hot, @RequestParam("pic") MultipartFile pic)
    {
        return RespJson.pack(200,"添加成功", hotService.addHot(hot,pic));
    }

    /**
     * 删除轮播图
     * @param id 被删除的ID
     * @return 进行全查询获取删除后的结果
     */
    @CacheEvict(value = "book",allEntries = true)
    @RequestMapping("/delete/{id}")
    public RespJson delete(@PathVariable("id")int id)
    {
        hotService.delete(id);
        return RespJson.pack(200,"删除成功",hotService.findAll());
    }
}
